﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class CreateNewProject : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int merchantId = 1;
            if (Session["MerchantId"] != null)
            {
                merchantId = int.Parse(Session["MerchantId"].ToString());
            }
            if (!Page.IsPostBack)
            {
                this.ddlGroup.DataSource = ServiceLocator.IClientService.GetNxGroupCollection(merchantId);
                this.ddlGroup.DataTextField = "GroupName";
                this.ddlGroup.DataValueField = "GroupId";
                this.ddlGroup.DataBind();
                this.ddlGroup.Items.Insert(0, new ListItem((String)GetGlobalResourceObject("Default", "ALL_CUSTOMERS"), "0"));
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtProjectName.Text.Trim() != "" && txtProjectDescription.Text.Trim() != "" && txtStartTime.Text.Trim() != "" && txtEndTime.Text.Trim() != "" && txtMMSTitle.Text.Trim() != "")
            {
                if (Convert.ToDateTime(txtStartTime.Text.Trim()) <= Convert.ToDateTime(txtEndTime.Text.Trim()))
                {
                    NxProject nxProject = new NxProject();
                    nxProject.ProjectName = txtProjectName.Text.Trim();
                    if (Session["MerchantId"] != null)
                    {
                        nxProject.MerchantId = int.Parse(Session["MerchantId"].ToString());
                    }
                    nxProject.ProjectDescribe = txtProjectDescription.Text.Trim();
                    nxProject.StartTime = Convert.ToDateTime(txtStartTime.Text.Trim());
                    nxProject.EndTime = Convert.ToDateTime(txtEndTime.Text.Trim());
                    nxProject.GroupId = int.Parse(ddlGroup.SelectedValue);
                    nxProject.MmsTitle = txtMMSTitle.Text.Trim();
                    nxProject.Status = 1;
                    if (ServiceLocator.IProjectService.InsertNxProject(nxProject))
                    {
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "CREATE_A_MMS") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "','MMSCreate.aspx?MmsPage=1&ProjectId=" + nxProject.ProjectId.ToString() + "')", this.Page);
                    }
                    else
                    {
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "CREATE_PROJECT_FAILED") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                    }
                }
                else
                {
                    JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "DATE_ERROR") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                }
            }
            else
            {
                JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "CAN_NOT_BE_EMPTY") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
            }
            
        }

    }
}
